package sysu.el.array;

public class BoolArray {
	private short boolArray[];
	private int size;
	private short trueFlag;
	public void init(){
		this.trueFlag = Short.MIN_VALUE + 1;
		for (int i=0; i<this.size; i++)
			this.boolArray[i] = Short.MIN_VALUE;
	}
	
	public void reset(){
		if (trueFlag == Short.MAX_VALUE){
			this.init();
		}else{
			this.trueFlag++;
		}
	}
	
	public BoolArray(int size){
		this.size = size;
		this.boolArray = new short[size];
		/** int[] initial value is zero. It is guaranteed in Java Specification		 */
		this.trueFlag = 1;
//		this.init();
	}
	
	public boolean get(int i){
		return (this.boolArray[i] == this.trueFlag);
	}
	
	public boolean isTrue(int i){
		return (this.get(i));
	}
	
	public boolean isFalse(int i){
		return this.boolArray[i] != this.trueFlag;
	}
	
	public void setTrue(int i){
		this.boolArray[i] = this.trueFlag;
	}
	
	public void setFalse(int i){
		this.boolArray[i] = (short) (this.trueFlag - (short)(1));
	}
	
	public boolean isVisited(int i){
		return this.isTrue(i);
	}
	
	public boolean isUnvisited(int i){
		return this.isFalse(i);
	}
	
	public void setVisited(int i){
		this.setTrue(i);
	}
	
	public void setUnvisited(int i){
		this.setFalse(i);
	}
	
	public String toString(){
		StringBuffer sb = new StringBuffer();
		for (int i=0; i<boolArray.length; i++){
			if (isFalse(i))
				sb.append("0");
			else
				sb.append("1");
			sb.append(" ");
		}
		return sb.toString();
	}
			
}

